/**
 * Created by jocky on 13/08/2017.
 */
var realNameCheckObj = {
    /**
     * 实名审核记录
     *
     */
    lists: function () {
        //显示loading
        show_loading_table('real_name_check_table', 5);

        var params = {};
        params.isRender = 2;//ajax调用

        $.each($('#real_name_check_search_form').find('input,select'), function (i, v) {
            var name = $.trim($(v).attr('name'));
            if (name != '') {
                params[name] = $(v).val();
            }
        });

        $.getJSON('/real-name-check', params, function (json) {
            var tr = "";
            $.each(json, function (i, v) {
                var status;
                switch (parseInt(v.update_status)) {
                    case 1:
                        status = "<label class='label label-danger'>未审核</label>";
                        break;
                    case 2:
                        status = "<label class='label label-ysh' style='background-color:green'>已审核</label>";
                        break;
                    case 3:
                        status = "<label class='label label-default'>已拒绝 </label>";
                        break;
                    case 9:
                        status = "<label class='label label-default'>永久拒绝 </label>";
                        break;    
                    default:
                        status = "";
                        break;
                }
                tr += "<tr>";
                tr += "<td><a href='javascript:void(0)' title='查看 / 修改账户信息' onclick=\"traderObj.setForm(" + v.userID + ")\">" + v.userID + "</a></td>";
                tr += "<td>" + v.loginName + "</td>";
                tr += "<td>" + v.accountName + "</td>";
                tr += "<td>" + convertDate(v.update_time, 1) + "</td>";
                tr += "<td>" + status + "</td>";
                tr += "<td class='text-center'><a href='javascript:void(0)' onclick=\"realNameCheckObj.detail(" + v.userID + ")\"> 查看</a></td>";
                tr += "</tr>";
            });

            if (tr == "") {
                tr += "<tr><td colspan='6' class='text-center'>暂无记录</td></tr>";
            }


            $("#real_name_check_table>tbody").html(tr);
        })
    },

    /**
     * 实名详情
     */
    detail: function (uid) {

        $.get('/real-name-check/detail', {userID: uid}, function (html) {
            $('#modal_pop2_title').html('<i>实名信息</i>');

            $('#modal_pop2_body').html(html);
            $('#modal_pop2').modal('show');
        })
    },
    /**
     *修改实名图片信息操作
     *
     * @param uid
     * @param img_id
     */
    imgmodify:function(uid,img_id) {
        var params = {};
        var img_s = document.getElementById("imgs_" + img_id).src;
        var img = new Image();
        img.src = img_s;

        var width = img.width;
        var height = img.height;
        var maxWidthSize = 800;
        var scale = 1;
        if (width > maxWidthSize || height > maxWidthSize) {
            if (width > height) {
                scale = maxWidthSize / width;
            } else {
                scale = maxWidthSize / height;
            }
        }

        if (scale == 1) {
            imageS = img_s;
        } else {
            var canvas = document.createElement("canvas");
            var ctx = canvas.getContext("2d");

            canvas.width = scale * width;
            canvas.height = scale * height;
            ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
            var nData = canvas.toDataURL('image/jpeg');
            imageS = nData;}
            params['userID']=uid;
            params['data']=img_id;
            params['imgs']=imageS;
            console.log(params);
            //{userID: uid,data:img_id,imgs:imageS}
        $.post('/real-name-check/images',params, function (json) {
            imageS = nData;
        params['userID'] = uid;
        params['data'] = img_id;
        params['imgs']=imageS;
        console.log(params);
        //{userID: uid,data:img_id,imgs:imageS}
            $.post('/real-name-check/images',params, function (json) {
            var obj = {
                title: '操作通知',
                text: json.msg,
                styling: 'bootstrap3',
                delay: 5000,
                type: ''
            };

            if (json.code == 1) {
                obj.type = 'info';
                obj.addclass = 'dark';
            } else {
                obj.type = 'error';
            }
            new PNotify(obj);
            realNameCheckObj.lists();
        },'json')
            }, 'json')
    },
    /**
     *修改实名信息审核操作
     *
     * @param uid
     * @param type 0拒绝，1通过
     */
    operate: function (uid, type) {
        var title = type == 1 ? '确定修改此实名信息吗？' : '确定拒绝此实名信息吗？';
        var errorMsg = $.trim($('#realNameCheckErrorMsg').val());
        if (type == 0 && errorMsg == '') {
            var obj = {
                title: '操作通知',
                text: '请填写拒绝原因',
                styling: 'bootstrap3',
                delay: 5000,
                type: 'error'
            };
            new PNotify(obj);

            return;
        }

        swal({
            title: title,
            text: " ",
            type: 'warning',
            showCancelButton: true,
            cancelButtonColor: '#d33',
            cancelButtonText: '取消',
            confirmButtonColor: '#3085d6',
            confirmButtonText: '确定'
        }).then(function () {
            //将修改的实名信息存放到数组
            var data=[];
             data[1]=$("input[name='71']").val();
             data[2]=$("input[name='74']").val();
             data[3]=$("input[name='81']").val();
             data[4]=$("input[name='82']").val();
             data[5]=$("input[name='88']").val();
             data[6]=$("input[name='89']").val();
             data[7]=$("input[name='90']").val();
            var str = data.toString();
            $.getJSON('/real-name-check/modify', {userID: uid,data:str}, function (json) {
                var obj = {
                    title: '操作通知',
                    text: json.msg,
                    styling: 'bootstrap3',
                    delay: 5000,
                    type: ''
                };

                if (json.code == 1) {
                    obj.type = 'info';
                    obj.addclass = 'dark';
                } else {
                    obj.type = 'error';
                }
                new PNotify(obj);
                realNameCheckObj.lists();
            })
        }).catch(swal.noop);

    },
    /**
     *实名信息审核操作
     *
     * @param uid
     * @param type 0拒绝，1通过
     */
    operates: function (uid, type) {
        var title="";
        if (type==9){
            title="确定永久性拒绝此用户的实名信息吗？";
        }else {
            title = type == 1 ? '确定通过此实名信息吗？' : '确定拒绝此实名信息吗？';
        }
        var errorMsg = $.trim($('#realNameCheckErrorMsg').val());
        if (type == 0 && errorMsg == '') {
            var obj = {
                title: '操作通知',
                text: '请填写拒绝原因',
                styling: 'bootstrap3',
                delay: 5000,
                type: 'error'
            };
            new PNotify(obj);

            return;
        }

        swal({
            title: title,
            text: " ",
            type: 'warning',
            showCancelButton: true,
            cancelButtonColor: '#d33',
            cancelButtonText: '取消',
            confirmButtonColor: '#3085d6',
            confirmButtonText: '确定'
        }).then(function () {
            $.getJSON('/real-name-check/operate', {userID: uid, type: type, error_msg: errorMsg}, function (json) {
                var obj = {
                    title: '操作通知',
                    text: json.msg,
                    styling: 'bootstrap3',
                    delay: 5000,
                    type: ''
                };
                console.log($.getJSON);
                if (json.code == 1) {
                    obj.type = 'info';
                    obj.addclass = 'dark';
                } else {
                    obj.type = 'error';
                }
                new PNotify(obj);

                $('#modal_pop2').modal('hide');
                realNameCheckObj.lists();
            })
        }).catch(swal.noop);

    }
};

